package leetCode;

/**
 * leet code8
 * 给定两个以字符串形式表示的非负整数 num1 和 num2，返回 num1 和 num2 的乘积，
 * 它们的乘积也表示为字符串形式。
 * @author 86155
 * @date 2023/12/13
 */
public class LeetCode8 {
    public static String multiply(String num1, String num2) {
        int n = num1.length();
        int m = num2.length();
        int[] res = new int[n + m];
        for (int i = n - 1; i >= 0; i--) {
            for (int j = m - 1; j >= 0; j--) {
                int a = num1.charAt(i) - '0';
                int b = num2.charAt(j) - '0';
                int r = a*b;
                //模拟乘法
                r += res[i + j + 1];
                res[i+j+1] = r%10;
                res[i+j] += r/10;
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < n + m; i++) {
            if (stringBuilder.length() == 0 && res[i] == 0) {
                continue;
            }
            stringBuilder.append(res[i]);
        }
        return stringBuilder.length() == 0?"0":stringBuilder.toString();
    }
}
